<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vue</title>
  <script src="../js/vue.js"></script>
  <script src="../js/vue-router.js"></script>
  <script src="../js/vuex.js"></script>
</head>
<body>
  <div id="app">
    <button @click="SET_AGE(10)">点击改变age</button>
    {{name}}:{{age}}
  </div>
  <script>
    let { mapState, mapGetters, mapMutations, mapActions } = Vuex
    // 1.声明vuex模块化对象
    let moduleA = {
      namespaced: true,
      state: {
        name: '张三'
      },
      getters: {},
      mutations: {

      },
      actions: {

      }
    }
    let moduleB = {
      namespaced: true,
      state: {
        age: 20
      },
      getters: {},
      mutations:{
        SET_AGE(state,payload) {
          state.age = payload
        }
      },
      actions: {}
    }
    // 2.声明vuex实例，将模块化对象作为参数传递进去
    let store = new Vuex.Store({
      modules: {
        moduleA,
        moduleB
      }
    })
    let vm = new Vue({
      el: '#app',
      // 3.将vuex实例store注入到vue实例中
      store,
      data: {},
      computed: {
        ...mapState('moduleA',['name']),
        ...mapState('moduleB',['age']),
      },
      methods: {
        ...mapMutations('moduleB',['SET_AGE']),
      },
    })
  </script>
</body>
</html>